package com.example.producerdemo.config;

import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.UUID;

/**
 * @author xzc
 */
@Configuration
public class RocketMqConfig {
    private static final Logger LOGGER = LoggerFactory.getLogger(RocketMqConfig.class);
    @Value("${rocketMQ.namesrvAddress}")
    protected String nameServer;
    @Value("${rocketMQ.groupName}")
    protected String groupName;
    @Value("${rocketMQ.maxMessageSize}")
    protected int maxMessageSize;

    @Bean
    public DefaultMQProducer producer() {
        DefaultMQProducer sender = new DefaultMQProducer(groupName);
        sender.setNamesrvAddr(nameServer);
        sender.setInstanceName(UUID.randomUUID().toString());
        sender.setMaxMessageSize(maxMessageSize);
        try {
            sender.start();
        } catch (MQClientException e) {
            e.printStackTrace();
        }
        LOGGER.info("RocketMq defaultProducer Started.");
        return sender;
    }
}